package com.bawei.rk_01.address

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.bawei.base.utils.mvvm.BaseAcitivity
import com.bawei.base.utils.param.Address
import com.bawei.base.utils.param.Paddress
import com.bawei.rk_01.R
import com.bawei.rk_01.databinding.ActivityAddAddressBinding
import com.blankj.utilcode.util.ToastUtils
import com.github.gzuliyujiang.wheelpicker.AddressPicker
import com.github.gzuliyujiang.wheelpicker.annotation.AddressMode

@Route(path = "/app/AddADdressActivity")

class AddADdressActivity : BaseAcitivity<ActivityAddAddressBinding,AddressViewModel>() {



    override fun getViewModel(): Class<AddressViewModel> {
        return AddressViewModel::class.java
    }

    override fun getLayout(): Int {
        return R.layout.activity_add_address
    }

    override fun initView() {

        //初始化
        val address = Address()

        //点击省市区，弹出地址选择器
        binding.dz.setOnClickListener {
            val picker = AddressPicker(this)
            picker.setTitle("请选择地址")
            picker.setAddressMode(AddressMode.PROVINCE_CITY_COUNTY)
            picker.setOnAddressPickedListener { province, city, county ->
                binding.dz.text = "省：${province.name} - 市：${city.name} - 区： ${county.name}"
                //进行赋值
                address.province = province.name
                address.city = city.name
                address.district = county.name
            }
            picker.show()
        }

        //点击报错按钮
        binding.bt1.setOnClickListener {
            val paddress = Paddress()
            paddress.detail = binding.et3.text.toString()
            paddress.phone = binding.et2.text.toString()
            paddress.realName = binding.et1.text.toString()
            paddress.isDefault = binding.cb.isChecked
            paddress.address = address
            viewModel.address(paddress)
        }

    }

    override fun initData() {

        viewModel.addresssuccess.observe(this){
            if (it.code == 200){
                finish()
            }else{
                ToastUtils.showLong(it.message)
            }
        }

    }

}